#include "travelling.hpp"
#include "csv_struct.hpp"

float travelling_csv::total_km(int _plate_id, date _first_date, date _last_date, bool _include_last_date)
{
	float amount(0);
	csv_struct csv_file(file_list::travelling_filename());
	
	while(!csv_file.eof())
	{
		++csv_file;
		travelling_struct_csv row(csv_file.get_line());

		if(!check_parm(row, _plate_id, _first_date, _last_date, _include_last_date))
		{	
			continue;
		}	

		//std::cout <<"[traveling_csv.total_km] " << row << std::endl;
		amount += row.km();
	}
		
	std::cout << "[travelling.total_km] TIME INTERVAL = [" <<_first_date<<", " << _last_date << "]"<< std::endl;
	std::cout << "[travelling.total_km] TOTAL KM = "<<amount<< std::endl;
	return amount;
}


float travelling_csv::fuel_refund(int _plate_id, date _first_date, date _last_date, bool _include_last_date)
{
	float amount(0);
	csv_struct csv_file(file_list::travelling_filename());

	while(!csv_file.eof())
	{
		++csv_file;
		travelling_struct_csv row(csv_file.get_line());

		if(!check_parm(row, _plate_id, _first_date, _last_date, _include_last_date))
		{
			continue;
		}
		
		amount += row.fuel_refund(_plate_id);	
	}

	std::cout << "[travelling_csv.fuel_refund] TIME INTERVAL = [" << _first_date << ", " << _last_date << "]" << std::endl;
	std::cout << "[travelling_csv.fuel_refund] TOTAL REFUND =" << amount << " euro" <<std::endl;
	
	return amount;
}


float travelling_csv::estimated_fuel_consumption_physical(int _plate_id, date _first_date, date _last_date, bool _include_last_date)
{
	csv_struct csv_file(file_list::travelling_filename());
	float t(0);
		
	std::cout << "[travelling.estimated_fuel_consumption_liters] TIME INTERVAL = [" <<_first_date<<", " << _last_date << "]"<< std::endl;

	while(!csv_file.eof())
	{
		++csv_file;
		travelling_struct_csv row(csv_file.get_line());

		if(!check_parm(row, _plate_id, _first_date, _last_date, _include_last_date))
		{	
			continue;
		}	

		t+= row.fuel_consumption_physical(_include_last_date);

	}

	std::cout << "[travelling.estimated_fuel_consumption_liters] TOTAL ESTIMATED FUEL CONSUMPTION (LT) = "<< t << std::endl;

	return t;
}


float travelling_csv::estimated_fuel_consumption_financial(int _plate_id, date _first_date, date _last_date, bool _include_last_date)
{
	csv_struct csv_file(file_list::travelling_filename());
	float t(0);
		
	std::cout << "[travelling.estimated_fuel_consumption_cost] TIME INTERVAL = [" <<_first_date<<", " << _last_date << "]"<< std::endl;

	while(!csv_file.eof())
	{
		++csv_file;
		travelling_struct_csv row(csv_file.get_line());

		if(!check_parm(row, _plate_id, _first_date, _last_date, _include_last_date))
		{	
			continue;
		}	

		t+= row.fuel_consumption_financial(_include_last_date);
	}

	std::cout << "[travelling.estimated_fuel_consumption_cost] TOTAL ESTIMATED FUEL CONSUMPTION (EURO) = "<< t << std::endl;

	return t;
}


bool travelling_csv::check_parm(travelling_struct_csv& row, 
		int _plate_id, date _first_date, date _last_date, bool _include_last_date)
{
	if(row.car_id() != _plate_id){ return false;} 					// check plate Id
	if(row.travelling_date() < _first_date) { return false; }                        // discard any date before
	if(row.travelling_date() > _last_date ){ return false; }                         // discard any date after
	if(row.travelling_date() == _last_date && !_include_last_date){  return false; } // is the last date to be considered?
		
	return true;
}


float travelling_csv::kilometer_report(int _plate_id, boost::gregorian::date _first_date, boost::gregorian::date _last_date, bool _include_last_date, std::ofstream* fp)
{
	csv_struct csv_file(file_list::travelling_filename());
	std::vector<float> travelling_parms;
	float travelling_parm_totals[] = {0, 0, 0, 0, 0, 0, 0};
			
	while(!csv_file.eof())
	{
		++csv_file;
		travelling_struct_csv row(csv_file.get_line());

		if(!check_parm(row, _plate_id, _first_date, _last_date, _include_last_date))
		{	
			continue;
		}	

		travelling_parms = row.calc_travelling_parms(_include_last_date);
	
		std::cout << row.travelling_date(); 	
		std::cout << "\tkm = "<< travelling_parms[travelling_struct_csv::TRAVEL_KM] << "\ttarif = " << travelling_parms[travelling_struct_csv::UNIT_FUEL_REFUND];
		std::cout << "\tfuel refund (euro) = " << travelling_parms[travelling_struct_csv::FUEL_REFUND] << std::endl;

		std::cout << "\t(estimated fuel consumption (lt) = " << travelling_parms[travelling_struct_csv::ESTIMATED_FUEL_CONSUMP_PHYSICAL];
		std::cout << "\tlatest fuel price (euro/lt) = " << travelling_parms[travelling_struct_csv::LATEST_FUEL_COST];
		std::cout << "\testimated fuel consumption (euro) = " << travelling_parms[travelling_struct_csv::ESTIMATED_FUEL_CONSUMP_FINANCIAL];
		std::cout << "\testimated net refund (euro) = " << travelling_parms[travelling_struct_csv::ESTIMATED_NET_REFUND];
		std::cout << ")" << std::endl;

		travelling_parm_totals[travelling_struct_csv::TRAVEL_KM] += travelling_parms[travelling_struct_csv::TRAVEL_KM];
		travelling_parm_totals[travelling_struct_csv::FUEL_REFUND] += travelling_parms[travelling_struct_csv::FUEL_REFUND];
		travelling_parm_totals[travelling_struct_csv::ESTIMATED_FUEL_CONSUMP_PHYSICAL] += travelling_parms[travelling_struct_csv::ESTIMATED_FUEL_CONSUMP_PHYSICAL];
		travelling_parm_totals[travelling_struct_csv::ESTIMATED_FUEL_CONSUMP_FINANCIAL] += travelling_parms[travelling_struct_csv::ESTIMATED_FUEL_CONSUMP_FINANCIAL];
		travelling_parm_totals[travelling_struct_csv::ESTIMATED_NET_REFUND] += travelling_parms[travelling_struct_csv::ESTIMATED_NET_REFUND];

		if(!fp || !fp->is_open()) continue;

		*fp << row.travelling_date(); 	
		*fp << "\tkm = "<< travelling_parms[travelling_struct_csv::TRAVEL_KM] << "\ttarif = " << travelling_parms[travelling_struct_csv::UNIT_FUEL_REFUND];
		*fp << "\tfuel refund (euro) = " << travelling_parms[travelling_struct_csv::FUEL_REFUND] << std::endl;

		*fp << "\t(estimated fuel consumption (lt) = " << travelling_parms[travelling_struct_csv::ESTIMATED_FUEL_CONSUMP_PHYSICAL];
		*fp << "\tlatest fuel price (euro/lt) = " << travelling_parms[travelling_struct_csv::LATEST_FUEL_COST];
		*fp << "\testimated fuel consumption (euro) = " << travelling_parms[travelling_struct_csv::ESTIMATED_FUEL_CONSUMP_FINANCIAL];
		*fp << "\testimated net refund (euro) = " << travelling_parms[travelling_struct_csv::ESTIMATED_NET_REFUND];
		*fp << ")" << std::endl;


	}

	std::cout << "km = " << travelling_parm_totals[travelling_struct_csv::TRAVEL_KM] << "\tfuel refund (euro) = " << travelling_parm_totals[travelling_struct_csv::FUEL_REFUND];
	std::cout << "\t(estimated net fuel refund (euro) = " << travelling_parm_totals[travelling_struct_csv::ESTIMATED_NET_REFUND] <<")" <<std::endl;

	if(fp && fp->is_open())
	{
		*fp << "km = " << travelling_parm_totals[travelling_struct_csv::TRAVEL_KM] << "\tfuel refund (euro) = " << travelling_parm_totals[travelling_struct_csv::FUEL_REFUND];
		*fp << "\t(estimated net fuel refund (euro) = " << travelling_parm_totals[travelling_struct_csv::ESTIMATED_NET_REFUND] <<")" <<std::endl;
	}

	return travelling_parm_totals[travelling_struct_csv::TRAVEL_KM];
}

/*
travelling_csv::~travelling_csv()
{
	file = "";
}*/
